﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Controller;
namespace Web
{
    public partial class EditVendor : System.Web.UI.Page
    {
        Helper helper = new Helper();
        VendorEntity vendorEntity = new VendorEntity();
        VendorController vendorController = new VendorController();

        protected void Page_Load(object sender, EventArgs e)
        {

            BindVendor();

            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        private void BindVendor()
        {
            int vendorId = Convert.ToInt32(Request.QueryString["vendorId"]);
            vendorEntity = vendorController.findVendorByVendorId(vendorId);
            NameTextBox.Text = vendorEntity.Name;
            ShortNameTextBox.Text = vendorEntity.ShortName;
            AddressTextBox.Text = vendorEntity.Address;
            PhoneTextBox.Text = vendorEntity.Phone;
            FaxTextBox.Text = vendorEntity.Fax;
            EmailTextBox.Text = vendorEntity.Email;
            ContactPersonTextBox.Text = vendorEntity.ContactPerson;
            ZipCodeTextBox.Text = vendorEntity.ZipCode;
            NumberTextBox.Text = vendorEntity.Number.ToString();
            CodeTextBox.Text = vendorEntity.Code;
            RemarkTextBox.Text = vendorEntity.Remark;
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            vendorEntity = new VendorEntity();
            vendorEntity.Id = Convert.ToInt32(Request.QueryString["vendorId"]);
            vendorEntity.Name = NameTextBox.Text;
            vendorEntity.ShortName = ShortNameTextBox.Text;
            vendorEntity.Address = ShortNameTextBox.Text;
            vendorEntity.Phone = PhoneTextBox.Text;
            vendorEntity.Fax = FaxTextBox.Text;
            vendorEntity.Email = EmailTextBox.Text;
            vendorEntity.ContactPerson = ContactPersonTextBox.Text;
            vendorEntity.ZipCode = ZipCodeTextBox.Text;

            bool result = vendorController.UpdateVendor(vendorEntity);
            if (result == true)
            {
                Session["FlashMessage"] = "Success: Update Customer Successful!";
                Session["FlashMessageType"] = "success";
            }
            else
            {
                Session["FlashMessage"] = "Error: Update Customer unsuccessful!";
                Session["FlashMessageType"] = "error";
            }
        }



        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/VendorList.aspx");
        }
    }
}